#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "const.h"

int Process(char *user_nm);


int main(int argc, char *argv[])
{
    int times, count;
    char user_nm[21];
    time_t start, end;

    if(argc!=2)  times=100;
    else  times=atoi(argv[1]);
    if(times==0)  times=100;

    if(DbsConnect())  exit(1);

    time(&start); 
    for(count=0; count<times; count++)
    {
        sprintf(user_nm, "test%04d", random_int(0,9999));
        if(Process(user_nm))
        {
            printf("bb_join query %d times fail\n", times);
            exit(1);
        }
        printf("user no.%04d %s finished\n", count+1, user_nm);
    }
    time(&end);
    printf("time cost total:%d seconds\n", end-start);

    exit(0);
}


int Process(char *user_nm)
{
    int result, count;
    double old_fee_id=0;
    charge_fee_t ChargeFee;
    charge_fee_dtl_t ChargeFeeDtl;

    count=0;
    strcpy(ChargeFee.status, CHARGE_FEE_STATUS_NOT_PAY);
    strcpy(ChargeFee.user_nm, user_nm);
    if(DbsCHARGE_FEE_CUR_BBJoin(DBS_OPEN, &ChargeFee,
           &ChargeFeeDtl)!=DB_OK)
    {   printf("open cursor fail\n");  return -1;   }
    while(1)
    {
        result=DbsCHARGE_FEE_CUR_BBJoin(DBS_FETCH, &ChargeFee,
                   &ChargeFeeDtl);
        if(result!=DB_OK && result!=DB_NOTFOUND)
        {   printf("fetch cursor fail\n");  return -1;   }
        if(result==DB_NOTFOUND)  break;

/*
        if((long)ChargeFeeDtl.fee_id!=(long)old_fee_id)
        {
            count++;
            if(count>20)  break; 
            old_fee_id=ChargeFeeDtl.fee_id;
        }
*/
    }
    if(DbsCHARGE_FEE_CUR_BBJoin(DBS_CLOSE, &ChargeFee,
           &ChargeFeeDtl)!=DB_OK)
    {   printf("close cursor fail\n");  return -1;   }

    return 0;
}
